package com.matt.controller.api.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.model.StatusResponse;
import com.matt.remoteCall.SsoCallService;
import com.matt.service.StatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * ****************************
 * description:统计控制器
 * Created by Ron
 * from com.matt
 * on 2018/6/29 0029.
 * ***************************
 */
@RestController
@RequestMapping(value = "/web/statistics")
@Api(tags = "CRM - 统计（顾问）")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private SsoCallService ssoCallService;

    /**
     * 统计新增记录数
     * @return
     */
    @ApiOperation(value = "线索新增数量、客户新增数量、跟进新增数量、合同新增数量、合同总金额、已预约客户、待面试客户数、待补款客户、待续约客户", httpMethod = "GET")
    @RequestMapping(value = "/new_record")
    public StatusResponse newRecord(@RequestHeader String ticket,  String beginDate, String endDate) throws ParseException {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();
        return StatusResponse.success(statisticsService.findNewRecords(areaId, beginDate, endDate));
    }

    /**
     * 统计图表的
     *
     * @param ticket
     * @param beginDate
     * @param endDate
     * @return
     */
    @GetMapping(value = "/count_record")
    @ApiOperation(value = "统计图表， 新合同数", httpMethod = "GET")
    public StatusResponse countRecord(@RequestHeader String ticket, String beginDate, String endDate) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();
        List<StaffDTO> staffDTOList = ssoCallService.getStaffDTOList(staff.getAreaId());
        return StatusResponse.success(statisticsService.countRecord(staffDTOList, areaId, beginDate, endDate));
    }


    //业绩完成度
    @GetMapping(value = "/complete_rate")
    @ApiOperation(value = "业绩完成度", httpMethod = "GET")
    public StatusResponse completeRate(@RequestHeader String ticket, String date) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();
        return StatusResponse.success(statisticsService.completeRate(staff.getId(), areaId, date));
    }


}
